/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ContentManagers.RoleManagers;

import ContentManagers.Security.Authenticator;
import ContentManagers.Models.Customer;
import DBConnection.ConnectionFactory;
import java.sql.*;

public class CustomerManager {
    
      public boolean checkCustomer(Customer customerLogin) {
        boolean result = false;

        try {
            ConnectionFactory myFactory = ConnectionFactory.getFactory("customer");
            Connection conn = myFactory.getConnection();
          
            int i = 1;

            PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM customer_accounts where username = ?");
            pstmt.setString(i++, customerLogin.getUsername());
            
            ResultSet rs = pstmt.executeQuery();

            while (rs.next()) {
                //System.out.print(customerLogin.getPassword() + rs.getString("password") + rs.getString("password_salt"));
                if(Authenticator.CompareHash(customerLogin.getPassword(), rs.getString("password"), rs.getString("password_salt")) == true)
                result = true;
               
               else result = false;
            }
            conn.close();
            
            }  catch (SQLException ex) {
                    ex.printStackTrace();
                }


        return result;
    }
      
      public boolean addCostumer(Customer newCustomer){
        boolean result = false;

         try{
            //opens db connection
            ConnectionFactory myFactory = ConnectionFactory.getFactory("customer");
            Connection conn = myFactory.getConnection();

            int i = 1;

            //SQL Query
            PreparedStatement pstmt = conn.prepareStatement("INSERT INTO customer (first_name, last_name, middle_init, username, "
                    + "password, email, billing_house_no, billing_street, billing_subdivision, "
                    + "billing_city, billing_postal_code, billing_country, delivery_house_no, "
                    + "delivery_street, delivery_subdivision, delivery_city, delivery_postal_code, "
                    + "delivery_country, credit_name, credit_no, credit_type, credit_exp_date, account_status, password_salt) "
                    + "VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");

            //include parameters
            pstmt.setString(i++, newCustomer.getFirstName());
            pstmt.setString(i++, newCustomer.getLastName());
            pstmt.setString(i++, newCustomer.getMiddleInitial());
            pstmt.setString(i++, newCustomer.getUsername());
            pstmt.setString(i++, newCustomer.getPassword());
            pstmt.setString(i++, newCustomer.getEmail());
            pstmt.setString(i++, newCustomer.getBillingHouseNo());
            pstmt.setString(i++, newCustomer.getBillingStreet());
            pstmt.setString(i++, newCustomer.getBillingSubdivision());
            pstmt.setString(i++, newCustomer.getBillingCity());
            pstmt.setString(i++, newCustomer.getBillingPostalCode());
            pstmt.setString(i++, newCustomer.getBillingCountry());
            pstmt.setString(i++, newCustomer.getDeliveryHouseNo());
            pstmt.setString(i++, newCustomer.getDeliveryStreet());
            pstmt.setString(i++, newCustomer.getDeliverySubdivision());
            pstmt.setString(i++, newCustomer.getDeliveryCity());
            pstmt.setString(i++, newCustomer.getDeliveryPostalCode());
            pstmt.setString(i++, newCustomer.getDeliveryCountry());
            pstmt.setString(i++, newCustomer.getCreditCardName());
            pstmt.setString(i++, newCustomer.getCreditCardNo());
            pstmt.setString(i++, newCustomer.getCreditCardType());
            pstmt.setString(i++, newCustomer.getExpirationDate());
            pstmt.setString(i++, "1"); //temporary initialization
            pstmt.setString(i++, newCustomer.getPasswordSalt()); //temporary salt value
            
            //other fields here

            //execute query
            pstmt.executeUpdate();

            //close DB connection
            conn.close();

            result=true;
            }
            catch(SQLException e)
            {
               e.printStackTrace();
            }
            return result;
    }
    
      public boolean resetPassword(Customer resetCustomerPassword){
          boolean result = false;
          
            try{
            //opens db connection
            ConnectionFactory myFactory = ConnectionFactory.getFactory("customer");
            Connection conn = myFactory.getConnection();

            int i = 1;
            
           
            //SQL Query
            PreparedStatement pstmt = conn.prepareStatement("UPDATE customer set password = ?, password_salt = ? WHERE username = ?");
            
            //include parameters
            pstmt.setString(i++, resetCustomerPassword.getPassword());
            pstmt.setString(i++, resetCustomerPassword.getPasswordSalt());
            pstmt.setString(i++, resetCustomerPassword.getUsername());
            
             //execute query
            pstmt.executeUpdate();

            //close DB connection
            conn.close();

            result=true;
            
            
            }catch(SQLException e)
            {
               e.printStackTrace();
            }
        
          
          return result;
      }
      
      
    
      public boolean checkIfExisting(String checkUsername) {
        boolean result = false;

        try {
            ConnectionFactory myFactory = ConnectionFactory.getFactory("customer");
            Connection conn = myFactory.getConnection();
          
            int i = 1;

            PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM customer_accounts where username = ?");
            pstmt.setString(i++, checkUsername);
            
            ResultSet rs = pstmt.executeQuery();

            while (rs.next()) {
                result = true;
            }
            conn.close();
            
            }  catch (SQLException ex) {
                    ex.printStackTrace();
                }


        return result;
    }
      
    public static int getCustomerID(String username)
    {   int customerID = -111;
        try {
            
            
            ConnectionFactory myFactory = ConnectionFactory.getFactory("customer");
            Connection conn = myFactory.getConnection();
          
            int i = 1;

            PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM customer where username = ?");
            pstmt.setString(i++, username);
            
            ResultSet rs = pstmt.executeQuery();

            while (rs.next()) {
                customerID = rs.getInt("customer_id");
            }
            
            conn.close();
            
            }  catch (SQLException ex) {
                    ex.printStackTrace();
                }
        return customerID;
    }
    
    public static ResultSet getPurchases(String username)
    {
        try {
            //get connection
            ConnectionFactory myFactory = ConnectionFactory.getFactory("customer");
            Connection conn = myFactory.getConnection();

            int i = 1;
            
            //Construct SQL Query
            PreparedStatement pstmt;
            
            pstmt = conn.prepareStatement("SELECT * from purchases WHERE username = ?");
            
            pstmt.setString(i++, username);
            
            //execute SQL statement
            ResultSet result = pstmt.executeQuery();

            return result;

            }
         catch (SQLException ex) {ex.printStackTrace();return null;}
    }
    
}
